package com.atguigu.linkedlist;

import java.util.Stack;

/**
 * @author shkstart
 * @create 2021-08-08 15:25
 */
public class SingleLinkedListDemo {
    public static void main(String[] args) {
    //进行测试
        //先创建节点
        HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
        HeroNode hero4 = new HeroNode(4, "林冲", "豹子头");
        //创建链表
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        //加入节点
        singleLinkedList.addByOrder(hero1);
        singleLinkedList.addByOrder(hero2);
        singleLinkedList.addByOrder(hero4);
        singleLinkedList.addByOrder(hero3);
        //反转链表
//        reversetList(singleLinkedList.getHead());
//        singleLinkedList.list();
//        reversePrint(singleLinkedList.getHead());
//        singleLinkedList.list();

////        singleLinkedList.addByOrder(hero2);
        //修改节点
        singleLinkedList.update(new HeroNode(2,"小卢","玉麒麟1133——"));
        singleLinkedList.list();
//        //删除节点
//        singleLinkedList.del(1);
//        singleLinkedList.del(4);
//        //显示数据
//        singleLinkedList.list();
//        //返回节点个数
//        System.out.println("有效的节点个数="+getLength(singleLinkedList.getHead()));
//        System.out.println("倒数的数据"+findLastIndexNode(singleLinkedList.getHead(),3));
    }
    //逆序输出
    public static void reversePrint(HeroNode head){
        if (head.next==null){
            return;
        }
        HeroNode cur=head.next;
        Stack<HeroNode> stack = new Stack<>();
        while (cur!=null){
            stack.push(cur);
            cur=cur.next;
        }
        while (stack.size()>0){
            System.out.println(stack.pop());
        }
    }
    //反转链表
    public static void reversetList(HeroNode head){
        if (head.next==null||head.next.next==null){
            return;
        }
        HeroNode cur=head.next;
        HeroNode next=null;
        //创建一个新的表头
        HeroNode reverseHead = new HeroNode(0, "", "");
        while (cur!=null){
            next=cur.next;
            cur.next=reverseHead.next;
            reverseHead.next=cur;
            cur=next;
        }
        head.next=reverseHead.next;
    }
    //获取倒数第几个节点
    public static HeroNode findLastIndexNode(HeroNode head,int index){
        if (head.next==null){
            return null;
        }
        int size = getLength(head);
        if (index<0||index>size){
            return null;
        }
        HeroNode cur=head.next;
        for (int i=0;i<size-index;i++){
            cur=cur.next;
        }
        return cur;
    }
    //获取节点个数
    public static int getLength(HeroNode head){
        if (head.next==null){
            return 0;
        }
        HeroNode cur=head.next;
        int length=0;
        while (cur!=null){
            length++;
            cur=cur.next;
        }
        return length;
    }


}
//定义SingleLinkedList管理我们的英雄
class SingleLinkedList{
    //初始化一个头节点
    private HeroNode head=new HeroNode(0,"","");
//返回头节点
    public HeroNode getHead() {
        return head;
    }

    //添加节点
    public void add(HeroNode heroNode){
        //头节点不能动，因此需要一个遍历节点辅助
        HeroNode temp=head;
        while (true){
            if (temp.next==null){
                break;
            }
            //节点往后移动
            temp=temp.next;

        }
        //找到最后的节点，赋值
        temp.next=heroNode;

    }
    //根据排名添加节点
    public void addByOrder(HeroNode heroNode){
        HeroNode temp=head;
        boolean flag=false;
        while (true){
            if (temp.next==null){
                break;
            }
            if (temp.next.no>heroNode.no){
                break;
            }else if (temp.next.no==heroNode.no){
                flag=true;
                break;
            }
            temp=temp.next;
        }
        if (flag){
            System.out.println("准备插入的英雄编号"+heroNode.no+"已经存在");
        }else {
            heroNode.next=temp.next;
            temp.next=heroNode;
        }
    }
    //修改节点
    public void update(HeroNode heroNode){
        if (head.next==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode temp=head;
        boolean flag=false;
        while (true){
            if (temp.next==null){
                break;
            }
            if (temp.next.no==heroNode.no){
                flag=true;
                break;
            }
            temp=temp.next;
        }
        if (flag){
            temp.next.name=heroNode.name;
            temp.next.nickname=heroNode.nickname;
        }else {
            System.out.println("没有找到对应编号"+heroNode.no);
        }
    }
//删除节点
    public void del(int no){
        HeroNode temp=head;
        boolean flag=false;
        while (true){
            if (temp.next==null){
                break;
            }
            if (temp.next.no==no){
                flag=true;
                break;

            }
            temp=temp.next;
        }
        if (flag){
            temp.next=temp.next.next;
        }else {
            System.out.println("没有找到对应的编号"+no);
        }
    }

    //遍历链表
    public void list(){
        //头节点是否为空
        if (head.next==null){
            System.out.println("链表为空");
        }
        //定义一个辅助变量
        HeroNode temp=head;
        while (true){
            if (temp.next==null){
                break;
            }
            System.out.println(temp.next.toString());
            temp=temp.next;
        }
    }
}
//定义HeroNode，每一个heroNode对象就是一个节点
class HeroNode{
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;
    //创建构造器
    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }
    //显示

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
